"""
角色模型
"""
from datetime import datetime
from app.extensions import db


class Role(db.Model):
    """角色模型"""
    __tablename__ = 'role'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.Text)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    # 多对多关系：角色-菜单
    menus = db.relationship('Menu', secondary='role_menu', backref='roles')
    
    def to_dict(self):
        """转换为字典"""
        return {
            'id': self.id,
            'name': self.name,
            'description': self.description,
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'menu_ids': [menu.id for menu in self.menus]
        }